from rest_framework import serializers
from apps.czb.models import Orders as CzbOrder
from apps.supply_order.models import SupplyOrder

orderModel = (("czb", "车主邦加油"),)


class BasePaySerializers(serializers.Serializer):
    supply_id = serializers.IntegerField(write_only=True, label="提交订单时返回的订单id",
                                         error_messages={"blank": "订单supply_id不能为空"}, help_text="系统内部订单id")
    redirect_url = serializers.CharField(write_only=True, label="重定向结果页面的url", required=True,
                                         error_messages={"blank": "重定向结果页面不能为空"}, help_text="前端重定向链接")
    pay_order_model = serializers.ChoiceField(write_only=True, required=False, allow_null=True, allow_blank=True,
                                              choices=orderModel, label="支付订单类型")
    channel = serializers.CharField(write_only=True, label="渠道", required=False, allow_null=True, allow_blank=True)

    def validate_supply_id(self, attrs):
        pay_order_model = self.initial_data["pay_order_model"]
        if pay_order_model == "czb":
            czb_order_instance = CzbOrder.objects.get(id=attrs)
            if czb_order_instance.result == 1:
                serializers.ValidationError("已经支付过了")
            if czb_order_instance.result == 2:
                serializers.ValidationError("此订单已退款，请重新下单")
            if czb_order_instance.result == 3:
                serializers.ValidationError("此订单有误，请重新下单")
            if czb_order_instance.result == 101:
                serializers.ValidationError("此订单已取消，请重新下单")
            setattr(self, "czb_order_instance", czb_order_instance)
        else:
            s_order = SupplyOrder.objects.get(id=attrs)
            setattr(self, "s_order", s_order)
        return attrs

    def update(self, instance, validated_data):
        pass

    def create(self, validated_data):
        pass
